package cn.topevery.garden.dal.logic.warning;

import cn.topevery.garden.dal.logic.base.WarningBase;
import cn.topevery.garden.entity.po.IotDeviceWaterQualityLog;
import cn.topevery.garden.framework.util.WarningConfigUtil;
import hw.topevery.basis.entity.po.SysClass;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：peng.tian
 * @date ：Created in 2021/9/26 11:37
 * @description：水质预警处理
 */
@Component
public class IotDeviceWaterQualityWarning extends WarningBase {
    @Override
    public String getClassId() {
        return "B7A53505-04AF-4509-B10C-892C64234540";
    }

    @Override
    public String getFacilitiesType() {
        return "7fbc583f-ee89-4e64-87d9-00e048581dab";
    }

    @Override
    public String getWarningType() {
        return "8DBB25AC-5AC8-4EC6-B86F-90A7D6DB0BBB";
    }

    public void check(IotDeviceWaterQualityLog data) {
        try {
            Map<String, String> flagMap = new HashMap() {
                {
                    put("水温传感器", data.getW01010flag());
                    put("PH传感器", data.getW01001flag());
                    put("电导率传感器", data.getW01014flag());
                    put("溶解氧传感器", data.getW01009flag());
                    put("浊度传感器", data.getW01003flag());
                    put("氨氮传感器", data.getW21003flag());
                    put("水位传感器", data.getW01099flag());
                }
            };

            List<SysClass> sysClassList = this.getWarningItems();
            Map filedsInfo = this.getFiledsInfo(data);
            StringBuilder warningDesc = new StringBuilder();
            if (sysClassList != null && sysClassList.size() > 0) {
                sysClassList.forEach(f -> {
                    this.setSmallClassId(f.classId);
                    Double threshold = this.getValue(f.classId);

                    if (f.code.equals("allFlag") && threshold.equals(1)) {
                        flagMap.keySet().forEach(key -> {
                            if (flagMap.get(key).equals("D")) {//设备异常
                                warningDesc.append(key + "异常\n");
                            }
                        });
                    } else {
                        if (filedsInfo.containsKey(f.code)) {
                            Double value = Double.parseDouble(filedsInfo.get(f.code).toString());
                            try {
                                if (WarningConfigUtil.Judge(value, threshold, f.remark)) {
                                    warningDesc.append(f.name + ",当前值:" + value.toString() + ",阈值:" + threshold.toString() + "\n");
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                if (warningDesc.toString().length() > 0) {
                    this.sendWarning(data.getDeviceCode(), warningDesc.toString(), data.getId());
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("水质水位预警检测异常!");
        }
    }
}
